home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / TESS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.8 KB  |  186 lines

  1. #include "stdio.h"
  2. #include <stdlib.h>
  3. #include <GL/glut.h>
  4.  
  5. static GLfloat spin = 0;
  6. static int level = 4;
  7. static int model = 0;
  8. static GLfloat rotx, roty;
  9. static int ox = -1, oy = -1;
  10. static int mot;
  11. #define PAN    1
  12. #define ROT    2
  13.  
  14. void
  15. movelight(int x, int y) {
  16.     spin += (x-ox);
  17.     ox = x; oy = y;
  18.     if (spin > 360.) spin -= 360.;
  19.     if (spin < -360.) spin -= -360.;
  20.     glutPostRedisplay();
  21. }
  22.  
  23. void
  24. rotate(int x, int y) {
  25.     rotx += x-ox;
  26.     if (rotx > 360.) rotx -= 360.;
  27.     else if (rotx < -360.) rotx += 360.;
  28.     roty += y-oy;
  29.     if (roty > 360.) roty -= 360.;
  30.     else if (roty < -360.) roty += 360.;
  31.     ox = x; oy = y;
  32.     glutPostRedisplay();
  33. }
  34.  
  35. void
  36. motion(int x, int y) {
  37.     if (mot == PAN) movelight(x, y);
  38.     else if (mot == ROT) rotate(x,y);
  39. }
  40.  
  41. void
  42. mouse(int button, int state, int x, int y) {
  43.     if(state == GLUT_DOWN) {
  44.     switch(button) {
  45.     case GLUT_LEFT_BUTTON:
  46.         mot = PAN;
  47.         motion(ox = x, oy = y);
  48.         break;
  49.     case GLUT_RIGHT_BUTTON:
  50.         mot = ROT;
  51.         motion(ox = x, oy = y);
  52.         break;
  53.     case GLUT_MIDDLE_BUTTON:
  54.         break;
  55.     }
  56.     } else if (state == GLUT_UP) {
  57.     mot = 0;
  58.     }
  59. }
  60.  
  61. void togglewire(void) {
  62.     static int toggle = 0;
  63.     toggle ^= 1;
  64.     glPolygonMode(GL_FRONT_AND_BACK, toggle ? GL_LINE : GL_FILL);
  65. }
  66.  
  67. extern void sphere(int level);
  68. void genmodel(void) {
  69.  
  70.     glNewList(1, GL_COMPILE);
  71.     if (model) {
  72.     GLUquadricObj *q = gluNewQuadric();
  73.     gluSphere(q, 1.0, 10*level, 10*level);
  74.     gluDeleteQuadric(q);
  75.     } else {
  76.     sphere(level-1);
  77.     }
  78.     glEndList();
  79. }
  80.  
  81. void togglemodel(void) {
  82.     model ^= 1;
  83.     genmodel();
  84. }
  85.  
  86. void levelup(void) {
  87.     level += 1;
  88.     if (level > 7) level = 7;
  89.     genmodel();
  90. }
  91.  
  92. void leveldown(void) {
  93.     level -= 1;
  94.     if (level <= 0) level = 1;
  95.     genmodel();
  96. }
  97.  
  98. void help(void) {
  99.     printf("'h'      - help\n");
  100.     printf("'t'      - tessellation style\n");
  101.     printf("'UP'     - increase tessellation\n");
  102.     printf("'DOWN'   - decrease tessellation\n");
  103.     printf("left mouse   - rotate sphere\n");
  104.     printf("right mouse  - move light\n");
  105. }
  106.  
  107. void init(void) {
  108.     GLfloat specular[4] = { 1., 1., 1., 1. };
  109.  
  110.     glEnable(GL_LIGHTING);
  111.     glEnable(GL_LIGHT0);
  112.     genmodel();
  113.     glDepthFunc(GL_LESS);
  114.     glEnable(GL_DEPTH_TEST);
  115.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
  116.     glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 30);
  117. }
  118.  
  119. void display(void) {
  120.     GLfloat position[] = { 0.0, 0.0, 3.5, 1.0 };
  121.  
  122.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  123.     glPushMatrix();
  124.     glTranslatef(0.0, 0.0, -5.0); 
  125.  
  126.     glPushMatrix();
  127.     glRotatef(spin, 1.0, 0.0, 0.0);
  128.     glRotatef(0.0, 1.0, 0.0, 0.0);
  129.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  130.  
  131.     glPopMatrix();
  132.  
  133.     glRotatef(rotx, 0., 1., 0.);
  134.     glRotatef(roty, 1., 0., 0.);
  135.     glCallList(1);
  136.     glPopMatrix();
  137.     glutSwapBuffers();
  138. }
  139.  
  140. void reshape(int w, int h) {
  141.     glViewport(0, 0, w, h);
  142.     glMatrixMode(GL_PROJECTION);
  143.     glLoadIdentity();
  144.     gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
  145.     glMatrixMode(GL_MODELVIEW);
  146. }
  147.  
  148. /*ARGSUSED1*/
  149. void
  150. key(unsigned char key, int x, int y) {
  151.     switch(key) {
  152.     case 't': togglemodel(); break;
  153.     case 'w': togglewire(); break;
  154.     case 'h': help(); break;
  155.     case '\033': exit(EXIT_SUCCESS); break;
  156.     default: break;
  157.     }
  158.     glutPostRedisplay();
  159. }
  160.  
  161. /*ARGSUSED1*/
  162. void
  163. special(int key, int x, int y) {
  164.     switch(key) {
  165.     case GLUT_KEY_UP:    levelup(); break;
  166.     case GLUT_KEY_DOWN:    leveldown(); break;
  167.     }
  168.     glutPostRedisplay();
  169. }
  170.  
  171. int main(int argc, char** argv) {
  172.     glutInit(&argc, argv);
  173.     glutInitWindowSize(512, 512);
  174.     glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
  175.     (void)glutCreateWindow(argv[0]);
  176.     init();
  177.     glutDisplayFunc(display);
  178.     glutKeyboardFunc(key);
  179.     glutSpecialFunc(special);
  180.     glutReshapeFunc(reshape);
  181.     glutMouseFunc(mouse);
  182.     glutMotionFunc(motion);
  183.     glutMainLoop();
  184.     return 0;
  185. }
  186.